package com.example.teaching_server.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

/**
 * 每日活动状态DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "每日活动状态信息")
public class DailyActivityStatusDTO {

    @NotNull(message = "活动ID不能为空")
    @Positive(message = "活动ID必须是正整数")
    @Schema(description = "活动唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long activityId;

    @NotBlank(message = "活动标题不能为空")
    @Schema(description = "活动标题", example = "春季科技创新营", requiredMode = Schema.RequiredMode.REQUIRED)
    private String title;

    @NotNull(message = "开始日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动开始日期", example = "2024-03-01", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDate startDate;

    @NotNull(message = "结束日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Schema(description = "活动结束日期", example = "2024-03-07", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDate endDate;

    @NotNull(message = "总天数不能为空")
    @Positive(message = "总天数必须大于0")
    @Schema(description = "活动总天数", example = "7", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer totalDays;

    @NotNull(message = "打卡状态不能为空")
    @Schema(description = "今日是否已打卡", example = "true", requiredMode = Schema.RequiredMode.REQUIRED)
    private Boolean isChecked;

    /**
     * 获取活动进行状态
     */
    @Schema(description = "活动状态", accessMode = Schema.AccessMode.READ_ONLY)
    public String getActivityStatus() {
        LocalDate today = LocalDate.now();
        if (today.isBefore(startDate)) {
            return "未开始";
        } else if (today.isAfter(endDate)) {
            return "已结束";
        } else {
            return "进行中";
        }
    }

    /**
     * 获取已进行天数
     */
    @Schema(description = "已进行天数", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getElapsedDays() {
        LocalDate today = LocalDate.now();
        if (today.isBefore(startDate)) {
            return 0;
        } else if (today.isAfter(endDate)) {
            return totalDays;
        } else {
            return (int) java.time.temporal.ChronoUnit.DAYS.between(startDate, today) + 1;
        }
    }

    /**
     * 获取剩余天数
     */
    @Schema(description = "剩余天数", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getRemainingDays() {
        return totalDays - getElapsedDays();
    }

    /**
     * 获取打卡进度百分比
     */
    @Schema(description = "打卡进度百分比", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getProgressPercentage() {
        if (totalDays == null || totalDays == 0) {
            return 0;
        }
        return Math.min(100, (getElapsedDays() * 100) / totalDays);
    }

    /**
     * 验证日期是否有效
     */
    public boolean isValidDateRange() {
        return startDate != null && endDate != null && !endDate.isBefore(startDate);
    }
}
